/*
 */

package com.railway.system.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.org.rapid_framework.page.Page;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.railway.common.base.BaseStruts2Action;
import com.railway.common.util.Json;
import com.railway.system.model.SysDept;
import com.railway.system.model.SysUser;
import com.railway.system.service.ISysDeptManager;
import com.railway.system.vo.query.SysDeptQuery;

/**
 * @author  jiangcheng
 * @version 1.0
 * @since 1.0
 */


public class SysDeptAction extends BaseStruts2Action implements Preparable,ModelDriven{
	
	//默认多列排序,example: username desc,createTime asc/
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String QUERY_JSP = "/admin/system/sysDept/query.jsp";
	protected static final String LIST_JSP= "/admin/system/sysDept/list.jsp";
	protected static final String CREATE_JSP = "/admin/system/sysDept/create.jsp";
	protected static final String EDIT_JSP = "/admin/system/sysDept/edit.jsp";
	protected static final String SHOW_JSP = "/admin/system/sysDept/show.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/system/SysDept/list.do";
	
	private ISysDeptManager sysDeptManager;
	
	private SysDept sysDept;
	java.lang.String id = null;
	private String[] items;
	private String param;
	
	public void setParam(String param){
		this.param = param;
	}

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			sysDept = new SysDept();
		} else {
			sysDept = (SysDept)sysDeptManager.getById(id);
			
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setSysDeptManager(ISysDeptManager manager) {
		this.sysDeptManager = manager;
	}	
	
	public Object getModel() {
		return sysDept;
	}
	
	public void setId(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	/** 执行搜索 */
	public String list() {
		return LIST_JSP;
	}
	public String showDeptTree(){
		
		return "/admin/system/sysDept/depttree.jsp";
	}
	public String getDeptTree() {		
		List list = new ArrayList();
		List<SysDept> deptList = sysDeptManager.findAll();

		if (deptList != null) {
			for (int i = 0; i < deptList.size(); i++) {

				SysDept sd = (SysDept) deptList.get(i);
				//				{id:1, pId:0, name:"部门01", open:true, nocheck:false,type:0},
				Map map = new HashMap();

				map.put("id", "D"+sd.getId());
				map.put("icon", "../../css/dept.gif");
				map.put("pId", sd.getParentDeptid());
				map.put("name", sd.getDeptName());
				map.put("open", "true");

				map.put("type", "0");
				list.add(map);

			}

		}

		return writeAjaxResponse(Json.toJson(list));

	}
	public String listAjax(){
		SysDeptQuery query = newQuery(SysDeptQuery.class,DEFAULT_SORT_COLUMNS);
		 
		//按odercode排序
		query.setSortColumns("order_code");
		Page page = sysDeptManager.findPage(query);
		Map map = new HashMap();
		map.put("total", page.getTotalCount());
		map.put("rows", page.getResult());
		return writeAjaxResponse(Json.toJson(map));
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	
	/** 保存新增对象 */
	public String save() {
		sysDept.setCreateTime(new Date());
		sysDeptManager.save(sysDept);
		return LIST_ACTION;
	}
	
	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}
	
	/**保存更新对象*/
	public String update() {
	   String deptid = this.sysDept.getId();
	   SysDept t = 	sysDeptManager.getById(deptid);
	   t.setDeptName(this.sysDept.getDeptName());
	   t.setOrderCode(sysDept.getOrderCode());
		sysDeptManager.update(t);
		return LIST_ACTION;
	}
	
	/**删除对象*/
	public String delete() {
		if(items!=null)
			for(String ids:items){
				String[] idArray = ids.split(",");
				for(String id:idArray)
				sysDeptManager.removeById(id);
			}
		return LIST_ACTION;
	}

}
